package com.sl.au.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.BusinesssellerBaseInfo;
import com.sl.au.entity.BussinessLevel;
import com.sl.au.entity.Module;
import com.sl.au.entity.User;
import com.sl.au.service.BusinesssellerBaseInfoService;
import com.sl.au.service.BussinessLevelService;
import com.sl.au.service.UserService;
import com.sl.utils.MsgHelper;

/**
 * 商户区域
 * 
 * @author yangdi
 *
 */
@RestController
@RequestMapping(value = "/ui/bussinessLevel")
public class BussinessLevelController extends BaseController {

		@Autowired
		BussinessLevelService bussinessLevelService;
		
		@Autowired
		BusinesssellerBaseInfoService  businesssellerBaseInfoService;
		
		@Autowired
		UserService userService;
	
		@RequestMapping(method = RequestMethod.GET)
		public ModelAndView index(){
			ModelAndView modelAndView = this.createLayoutView("bussinessLevel/bussinessLevelindex");
			List<User> users = userService.userByRole("社区管理员");
			modelAndView.addObject("users", users);
			return modelAndView;
		}
		
		//显示模型的所有节点和模型图片ID
		@RequestMapping(value="getAllNodes",method = RequestMethod.GET)
		public Map<String,Object> getAllNodes(HttpServletRequest request)
		{
			Map<String,Object> map = new HashMap<String,Object>();
			List<BussinessLevel> node=null;
			node=bussinessLevelService.getAllNodes();
			List<Map<String,Object>> nodeMap=new ArrayList<Map<String,Object>>();
			nodeMap=bussinessLevelService.getNodeMap(node);
			map.put("code", 1);
			map.put("nodes", nodeMap);

			return map;
		}
		
		///获得一个菜单的信息byID
		@RequestMapping(value="getNode",method = RequestMethod.GET)
		public Map<String,Object> getNode(HttpServletRequest request){
			Map<String,Object> map = new HashMap<String,Object>();
			String Id=request.getParameter("Id");
			BussinessLevel node = bussinessLevelService.getRepository().findOne(Id);
			map.put("node", node);
			return map;
		}
		
		
		@RequestMapping(value = "getpage", method = RequestMethod.POST)
		public Map<String,Object> getPage(HttpServletRequest request) {
			Map<String,Object> map = new HashMap<String,Object>();
			int start = Integer.parseInt(request.getParameter("start"));
			int length = Integer.parseInt(request.getParameter("length"));
			String key=request.getParameter("key");
//			String moduleName='%'+key+'%';
			List<BussinessLevel> modules=null;
			Long count=null;
			if(org.springframework.util.StringUtils.hasText(key)){
				modules = bussinessLevelService.getBussinessLevels(start, length, key, null, null);
				count=bussinessLevelService.getRepository().getBussinessLevelCount(key);
			}
			else{
				modules = bussinessLevelService.getPage(start, length, null, null, null);
				count=bussinessLevelService.getRepository().getCount();
			}
			map.put("data", modules);
			map.put("draw", request.getParameter("draw"));
			map.put("recordsFiltered", count);
			map.put("recordsTotal", count);
			return map;
		}
		
		@RequestMapping(value="/save",method = RequestMethod.POST)
		public MsgHelper saveBussinessLevel(@RequestBody Map<String,Object> map){
			MsgHelper msgHelper = MsgHelper.success();
			String id = (String) map.get("id");
			String levelName = (String) map.get("levelName");
			String phoneNumber = (String) map.get("phoneNumber");
			String managerId = (String) map.get("manager");
			User manager = userService.getRepository().findOne(managerId);
			int levelOrder = Integer.valueOf((String) map.get("levelOrder"));
			if(!id.equals("")){
				BussinessLevel bussinessLevelOld = bussinessLevelService.getRepository().findOne(id);
				if(bussinessLevelOld!=null){
					//bussinessLevelOld.setUpdateTime(new Date());
					bussinessLevelOld.setLevelName(levelName);
					bussinessLevelOld.setLevelOrder(levelOrder);
					bussinessLevelOld.setPhoneNumber(phoneNumber);
					//bussinessLevelOld.setManager(manager);
					bussinessLevelService.getRepository().save(bussinessLevelOld);
				}
			}else{
				BussinessLevel bussinessLevelNew = new BussinessLevel();
				bussinessLevelNew.setLevelName(levelName);
				bussinessLevelNew.setLevelOrder(levelOrder);
				bussinessLevelNew.setPhoneNumber(phoneNumber);
				//bussinessLevelNew.setManager(manager);
				bussinessLevelService.getRepository().save(bussinessLevelNew);
			}
			return msgHelper;
		};
		
		@RequestMapping("/delete/{id}")
		public MsgHelper deleteBussinessLevel(@PathVariable("id") String id) {
			MsgHelper msgHelper = MsgHelper.success();
			bussinessLevelService.getRepository().delete(id);
			return msgHelper;
		}
		
		@RequestMapping(value="/saveNode",method = RequestMethod.POST)
		public Map<String,Object> saveNode(@RequestBody Map<String,Object> map){
			Map<String,Object> msg = new HashMap<String,Object>();
			String id = (String) map.get("id");
			String parentId = String.valueOf(map.get("levelPId"));
			//String parentId = "0";
			BussinessLevel bussinessLevel = bussinessLevelService.getRepository().findOne(id);
			bussinessLevel.setLevelParentId(parentId);
			bussinessLevelService.getRepository().save(bussinessLevel);
			msg.put("code", 1);
			msg.put("BussinessLevel", bussinessLevel);
			return msg;
		};
		
		//删除模型的节点
		@RequestMapping(value="/deleteNodes")
		public Map deleteNode(@RequestBody Map<String,Object> map)
		{
			String str=String.valueOf(map.get("str"));
			//暂未使用
			//String selectPId= String.valueOf(map.get("selectPId"));
			String ids[]=str.split(",");
			//删除商户区域下面的商户
			businesssellerBaseInfoService.deleteBussinessLevel(ids);
			bussinessLevelService.deleteNodes(ids);
			map.put("code", 1);
			return map;
		}
}
